var zNodes = null;
var tab_counter = 0;
var rootPath = getRootPath();
var $tabs;
var currentNode;
var myLayout;
var setting = {
    view: {
        showLine: true,
        selectedMulti: false,
        dblClickExpand: false
    },
    callback: {
        onClick: function(e, treeId, node){
            currentNode = node;
            if (node.isParent) {
                zTree_Menu.expandNode(node);
            }
            else {
                if ($("#tabs-" + node.menu_id).length) {
                    $tabs.tabs('select', node.index);
                }
                else {
					tab_counter++;
                    node.index = tab_counter;
                    addTab(node.name, tab_counter, node.menu_id);
                    $tabs.tabs('select', tab_counter);
                }
            }
        }
    }
};
function addTab(name, tab_counter, id){
    var tab_title = name;
    $tabs.tabs('add', '#tabs-' + id, tab_title);
    tab_counter++;
}

function hideLoadingGif(){
    $("#pageloading").hide();
}
$(document).ready(function(){
	getMainTree();
    myLayout = $('body').layout({
        defaults: {
            resizerClass: "resizer",
            togglerClass: "toggler",
            buttonClass: "button",
            togglerLength_open: 25,
            togglerLength_closed: 25,
            onresize_end: function(){
				var iztree=$(window.frames["iframe-" + currentNode.menu_id].document).find(".ztree");
                var iframeLayout = $(window.frames["iframe-" + currentNode.menu_id].document).find("div.flexigrid");
                if (iframeLayout) {
						
					if(ztree){
                   		iframeLayout.width(window.frames["iframe-" + currentNode.menu_id].document.documentElement.clientWidth-iztree.width()-25);
					}else{
						iframeLayout.width(window.frames["iframe-" + currentNode.menu_id].document.documentElement.clientWidth);
					}
                }
            }
        },
        north: {
            spacing_open: 1,
            togglerLength_open: 0,
            togglerLength_closed: -1,
            resizable: false,
            slidable: false,
            fxName: "none"
        }
    });
    $("#tabs").tabs({
        scrollable: true,
		remove:function(){
			tab_counter--;
		}
    });
    $tabs = $('#tabs').tabs({
        tabTemplate: '<li><a href="#{href}">#{label}</a><span class="ui-icon ui-icon-close" style="float:left;">Remove Tab</span></li>',
        add: function(event, ui){
            $(ui.panel).css({
                "padding": 0,
                "margin": 0,
                "height": "90%"
            }).append('<iframe id="iframe-' + currentNode.menu_id + '" src="' + rootPath + currentNode.menu_url + '" style="border:none;width:100%;height:105%;margin:0;padding:0;"></iframe>');
        }
    });
    $('#tabs span.ui-icon-close').live('click', function(){
        var index = $('li', $tabs).index($(this).parent());
        $tabs.tabs('remove', index);
    });
    $("#logo").fadeTo(0, 0.75).css("margin-top", "20px");
    $("#mylogout").click(function(){
        window.location.href = rootPath + "/j_spring_security_logout";
    });
});
function getRootPath(){
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
}
function getMainTree(){
	$.ajax({
        type: "POST",
        url: rootPath + "/menu/getMenuTree.json",
        dataType: "json",
        cache: false,
        async: false,
        success: function(msg){
            zNodes = msg.msg;
            setTimeout(hideLoadingGif, 800);
        }
    });
	$.fn.zTree.init($("#tree"), setting, zNodes);
    zTree_Menu = $.fn.zTree.getZTreeObj("tree");
}
